package com.nzy.controller;

import com.nzy.common.ResultData;
import com.nzy.controller.request.CreateOrder;
import com.nzy.controller.request.ListOrdersRequest;
import com.nzy.entity.Orders;
import com.nzy.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单服务
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 下单
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public ResultData orderSubmit(CreateOrder createOrder){

        Integer number = ordersService.getStock(createOrder.getSkuId());
        if (number < createOrder.getNumber()){
            return ResultData.fail("库存不足！");
        }

        Orders orders = new Orders();
        BeanUtils.copyProperties(createOrder, orders);
        ordersService.submit(orders);
        return ResultData.success();
    }

    /**
     * 订单列表
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResultData orderQuery(ListOrdersRequest listOrdersRequest){
        return ResultData.success(ordersService.quesyList(listOrdersRequest));
    }

    /**
     * 订单详情，带商品详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResultData orderDetail(Integer id){
        return ResultData.success();
    }
}
